<template>
  <div class="table">
    <!--表2季度生产情况T_MulQuartProduc-->
    <div class="divSearch">
      <Row>
        <Col span="24" class="col-margin"> 填报年度：
          <Select v-model="searchModel.FillInYear" placeholder="全部" style="width: 100px" @on-change="getReportData()">
            <Option value="">全部</Option>
            <Option :value="item.DictText" v-for="item in CreateYearList" :key="item.DictValue">{{item.DictText}}</Option>
          </Select>
          <Button icon="ios-search" type="ghost" @click="getReportData()" class="ButtonStyle">
            查询
          </Button>
          <Button icon="ios-download" type="primary" @click="ExcelDown()" class="searchButton" style="float:right;width: 100px;">数据导出</Button>
          <Upload :on-success="handleSuccess" :action="GetPostUrl()" style="float:right;width: 100px;" v-show="ReportStatus <3">
            <Button type="primary" icon="ios-cloud-upload-outline" class="searchButton">数据导入</Button>
          </Upload>
          <Button icon="ios-cloud-download" type="primary" @click="TmpDown()" class="searchButton" style="width: 100px;">模板下载</Button>
          <!-- <label style="float:right;padding: 6px;font-size:16px;">当前状态:
            <span v-if="ReportStatus==0" class="Error"></span> 
            <span v-else-if="ReportStatus==1" class="NoReport">未填写</span>
            <span v-else-if="ReportStatus==2" class="AlreadyReport">已填写</span>
            <span v-else-if="ReportStatus==3" class="UpReport">已上报</span>
            <span v-else-if="ReportStatus==4" class="Success">审核通过</span>               
          </label> -->
        </Col>
      </Row>
    </div>
    <Row>
      <table class="ContentTable">
        <caption>
            <label style="float:left">{{searchModel.FillInYear}}表8企业信息调查表</label>
            <label style="float:right">当前状态:
              <Poptip trigger="hover" placement="left-start" v-model="visible" :content="ReportOpinion" v-if="ReportStatus==0">
                <span  class="Error">重新填写</span> 
              </Poptip>
              <span v-else-if="ReportStatus==1" class="NoReport">未填写</span>
              <span v-else-if="ReportStatus==2" class="AlreadyReport">已填写</span>
              <span v-else-if="ReportStatus==3" class="UpReport">已上报</span>
              <span v-else-if="ReportStatus==4" class="Success">审核通过</span>   
              <span v-else class="NoReport">无需填报</span>                 
            </label><br/>
             <label style="float:left;margin:5px">
               <Button icon="plus" type="ghost" size="small" @click="addItem" :disabled="ReportStatus < 3 && BtnSaveStatus ? false:true">新增</Button>
            </label>   
        </caption>
        <thead>
        <tr>
          <th >序号</th>
          <th >企业名称</th>
          <th >固定资产(万元)</th>
          <th >龙头企业级别</th>
          <th >缫丝能力(绪)</th>
          <th>丝绸产能(万米)</th>
          <th >蚕丝被产能(吨)</th>
          <th >其他产能</th>
          <th >备注</th>
          <th width="50px">操作</th>          
        </tr>
        </thead>
        <tbody  style="max-height:360px;">
        <tr v-for="(dataitem,index) in datalist" :key="dataitem.Id">
          <td>{{index+1}}</td>
          <td>
            <Input style="display:none;" v-model="dataitem.Id"></Input>
            <Input style="display:none;" v-model="dataitem.ReportId"></Input>
            <Input v-model="dataitem.CompanyName"></Input>
          </td>
          <td>
            <Input v-model="dataitem.FixedAssets"></Input>
          </td>
          <td>
            <Select v-model="dataitem.CompanyType" placeholder="龙头企业级别">
              <!-- <Option value="">全部</Option> -->
              <Option :value="item.DictText" v-for="item in companyTypeList" :key="item.DictValue">{{item.DictText}}</Option>
            </Select>
          </td>
          <td>
            <Input v-model="dataitem.ReelingAbility"></Input>
          </td>
          <td>
            <Input v-model="dataitem.SilkAbility"></Input>
          </td>
          <td>
            <Input v-model="dataitem.SilkQuiltAbility"></Input>
          </td>
          <td>
            <Input v-model="dataitem.OtherAbility" ></Input>
          </td>
          <td>
            <Input v-model="dataitem.Remark" placeholder="备注长度请小于150个字符..."></Input>
          </td>
          <td width="50px">
            <Button type="ghost" size="small" icon="ios-trash-outline" style="color:#FF0000" @click="delOp(dataitem)" :disabled="ReportStatus < 3 && BtnSaveStatus ? false:true">
            </Button>
          </td>
        </tr>
        </tbody>
      </table>
      <div class="divButton">
        <Button type="primary" @click="save()" :disabled="ReportStatus < 3 && BtnSaveStatus ? false:true">保存</Button>
        <Button type="primary" @click="DataUp()" :disabled="ReportStatus == 2 ? false:true">上报</Button>
      </div>
      <Alert banner type="error">
        <p style="color:red;">填报说明:</p>
        <p>备注：1.未列出的，其余相关蚕桑制品企业生产的产品产量填入“其它”，如桑叶茶、桑枝食用菌、桑葚酒等企业，并同时在备注里注明产品名称及产能；</p>
        <p> 2.龙头企业级别请填市级、省级、国家级。</p>
        <span slot="desc"></span>
      </Alert>
    </Row>
  </div>
</template>

<script>
  export default {
    data() {
      const validateNumber = (rule, value, callback) => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          if (value < 18) {
            callback(new Error('必须年满18岁'));
          } else {
            callback();
          }
        }
      };
      return {
        visible:true,
        ReportOpinion:'',
        datalist: [],
        datas: {}, //table数据对象
        CultivarList: this.GetGroupDict("CSCultivar"),
        CreateYearList: this.GetGroupDict("ReportYear"), //上报年份
        companyTypeList:this.GetGroupDict("CompanyType"),//
        CurrentYear: this.GetCurrentYear(), //当前年度
        ReportId: '',
        index:0,
        ReportStatus: '',
        BtnSaveStatus: false, //保存按钮开关
        //查询模型：根据需求删除不不要查询条件
        searchModel: {
          ReportCode: 'CompanyInfo',
          FillInYear: '',
          TimesType: '',
          AreaCode: this.GetlocaStorage("CSuserinfo").AreaCode,
        },
        clientHeight: window.innerHeight - 260, //表格默认高度
      }
    },
    mounted() {
      //table高度计算方法
      const that = this;
      window.onresize = () => {
        return (() => {
          that.clientHeight = window.innerHeight - 260;
        })()
      };
    },
    methods: {
      addItem() {
        var model = { //编辑模型
          Id: 0,
          ReportId: '',
          CompanyName: '',
          CompanyType:'',
          FixedAssets: '',
          ReelingAbility: '',
          SilkAbility: '',
          SilkQuiltAbility: '',
          OtherAbility: '',
          Remark: ''
        };
        model.ReportId = this.ReportId;
        model.Id = this.datalist.length + 1;
        this.datalist.push(model);
      },
      delOp(m) {
        for (var i = 0; i < this.datalist.length; i++) {
            if (this.datalist[i].Id == m.Id) {
                if(this.deleteData(m.Id))
                {
                    this.datalist.splice(i, 1);
                    self.$Notice.success({
                        title: '数据移除成功！',
                        desc: false
                    });
                }
                else{
                    this.datalist.splice(i, 1);
                }
            }
        }
      },
      deleteData(ID){
        var self=this;
        self.$http.delete("CompanyInfo",{params: {id: ID}}).then(
            function(res){
                if(res.data.status) {//删除成功
                    self.getReportData();
                    return true;
                }
                else{//删除失败
                    return false;
                }
            }
        )
      },
      //数据上报
      DataUp() {
        var self = this;
        self.$Modal.confirm({
          title: '提示信息',
          content: '<p>数据上报后，不能再更改，是否继续上报？</p>',
          onOk: () => {
            var model = new Object();
            model.Id = self.ReportId;
            model.reportType = '3';
            model.strMsg = '';
            this.$http.post("Report/PostUpStatus", model).then(
              function (res) {
                if (res.data.status == 1) {
                  self.$Notice.success({
                    title: '数据上报成功！',
                    desc: false
                  });
                  self.getReportData();
                } else {
                  self.$Modal.error({
                    title: "提示信息",
                    content: "数据上报失败!"
                  });
                }
              }
            );
          }
        });
      },
      getReportData() {
        var self = this;
        var user = this.GetlocaStorage("CSuserinfo");
        this.searchModel.AreaCode = user.AreaCode;
        if (self.searchModel.FillInYear != null && self.searchModel.FillInYear != '') {
          this.searchModel.FillInYear = self.searchModel.FillInYear;
        } else {
          this.searchModel.FillInYear = self.CurrentYear + '年'; //填报年度-----数据字典值
        }
        self.$http.post('Report/PostPageList', this.searchModel).then(function (res) {
          if (res.data.status === 1) {
            if (res.data.rows.length > 0) {
                self.ReportId = res.data.rows[0].Id;
                self.ReportStatus = res.data.rows[0].Status;
                self.BtnSaveStatus = true;
            } else {
                self.ReportId = '';
                self.ReportStatus = '';
                self.BtnSaveStatus = false;
            }
            self.getPageData(self.ReportId);
          } else {
            self.$Modal.error({
              title: '提示信息',
              content: res.data.message
            });
          }
        });
      },
      //获取this.datas: {},//table数据对象
      getPageData(ReportId) {
        var self = this;
        var reportCode = self.searchModel.ReportCode;
        this.$http.get("Report", {
          params: {
            id: ReportId,
            reportCode: reportCode
          }
        }).then( //获取数据编辑项的值
          function (res) {
            switch (res.data.list.status) {
              case 1:
                self.ReportOpinion=res.data.Opinion;
                self.datalist = res.data.list.model;
                break;
              case 0:
                self.$Modal.error({
                  title: "提示信息",
                  content: "数据加载失败!!!"
                });
                break;
              case -1:
                self.$Modal.error({
                  title: "提示信息",
                  content: "数据加载失败!!!"
                });
                break;
            }
          }
        );
      },
      checkEmpty(value){
        var reg = /\S/;
        var temp = /^[0-9]\d*(\.\d+)?$/;//数字验证
        var p=/^[0-9]+([.]{1}[0-9]{1})?$/;//匹配整数或带一个小数
        if (reg.test(value)) {
          if (temp.test(value) == false) 
          {
            return false;
          }
          else {
            if(p.test(value)==false){
              return false;
            }
            else{
              return true;
            }
          }
        } 
        else {
          return false;
        }
      },
      checkName(value){
        var reg = /\S/;
        if (reg.test(value)&&value!=null) {
          return true;
        } 
        else {
          return false;
        }
      },
      //保存方法
      save() {
        var self = this;
        var m=0;
        for(var i=0;i<self.datalist.length;i++)
        {
            if(self.checkName(self.datalist[i].CompanyName)&&self.checkName(self.datalist[i].CompanyType)
            &&self.checkEmpty(self.datalist[i].FixedAssets)&&self.checkEmpty(self.datalist[i].ReelingAbility)
            &&self.checkEmpty(self.datalist[i].SilkAbility)&&self.checkEmpty(self.datalist[i].SilkQuiltAbility)
            &&self.checkEmpty(self.datalist[i].OtherAbility)){
              m ++;
          }
        }
        if(m==self.datalist.length){
          this.$http.post('CompanyInfo', self.datalist).then(
          function (res) {
            if (res.data.status == 1) {
              self.getReportData();
              self.index=0;
              self.$Notice.success({
                title: '保存成功！',
                desc: false
              });
            } else {
              self.$Modal.warning({
                title: "提示信息",
                content: "更新失败,请与管理员联系!"
              });
            }
          });
        }else{
          this.$Notice.error({
              title: '数据有误,请检查后再试!',
              desc: '',
            });
        }
      },
      //导入
      GetPostUrl() {
        var url = this.$http.defaults.baseURL + "Report/PostImportData?FillInYear="+this.searchModel.FillInYear+"&&ReportCode=" + this.searchModel.ReportCode +
          "&&AreaCode=" + this.searchModel.AreaCode;
        return url;
      },
      handleSuccess(res, file) {
        var self = this;
        self.getReportData();
        self.$Notice.success({
          title: '数据导入成功！',
          desc: false
        });
      },
      //导出
      ExcelDown: function () {
        var self = this;
        var model = new Object();
        model.ReportCode = self.searchModel.ReportCode;
        if (self.searchModel.FillInYear != null && self.searchModel.FillInYear != '') {
          model.FillInYear = self.searchModel.FillInYear;
        } else {
          model.FillInYear = self.CurrentYear + '年'; //填报年度-----数据字典值
          self.searchModel.FillInYear = self.CurrentYear + '年';
        }
        model.AreaCode = self.searchModel.AreaCode;
        window.open(this.$http.defaults.baseURL + "CompanyInfo/GetExcelData?ReportCode=" + model.ReportCode +
          "&&FillInYear=" + model.FillInYear + "&&AreaCode=" + model.AreaCode);
      },
      //模板下载
      TmpDown: function () {
        window.open(this.$http.defaults.baseURL + "Report/GetTemplateData?ReportCode=" + this.searchModel.ReportCode);
      },
    },
    //创建Vue页面方法
    created: function () {
      //创业Vue前调用获取this.datas: {},//table数据对象
      this.getReportData();
    },
    watch: {
      //需要监听的在此处新增监听对象
    }
  }

</script>
<style scoped src="../../../styles/stats.css"></style>